﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

// std headers
#include <memory>

// Qt headers
#include <QWidget>

class QMargins;
class QColor;
class QLineEdit;
class QComboBox;
class QPushButton;
class QLayout;


namespace gmepgcs
{
    struct IProjectComponent;
    struct IDetailedProjectComponent;
    class Gac3DViewer;
    struct ILegend;
    class LegendEditingCard;
    class EdittingParametersTableView;
    struct IParameter;
    class ParametersEditingCard;
    class EditingProjectComponentContext;
    class CustomScroller;
}

namespace gmepgcs
{
    class ProjectComponentEditingPage : public QWidget
    {
        Q_OBJECT    
    public:
        explicit ProjectComponentEditingPage(QWidget *parent = nullptr, bool isPrivateEnv = false);
        virtual ~ProjectComponentEditingPage();

    public:
        void CheckIn(const std::wstring &projectId, const std::wstring &enterpriseId, const std::wstring &componentId, const int32_t &componentVersion, const std::wstring &componentName);
        void SetIsByCommandShow(bool bIsByCommandShow);
        void SetComponentCurrentFamilyTypeName(const std::wstring &familyTypeName);

    signals:
        void OnProjectComponentEditingPageRequired();
        void EditingFinished(const std::wstring &projectId, const std::wstring &componentId, const int32_t &componentVersion,const bool &isCommandShow = false);

    protected:
        virtual void paintEvent(QPaintEvent *event) override;

    private:
        void InitUI(); 
        QLayout *InitBottomButtonsRegion();
        QWidget *InitLegendCardsRegion();
        QLayout *InitComponentNameAndCategoryRegion();
        QLayout *InitSpecificationsRegion();
        QWidget *InitGeometryParametersRegion();
        QWidget *InitProductParametersRegion();
        void InitStandardParametersCard();


        void Resume();

        void Prepare(const std::wstring &projectId, const std::wstring &enterpriseId, const std::wstring &componentId, const int32_t &componentVersion, const std::wstring &componentName);

        void PopulateData();
        void PopulateComponentName();
        void PopulateCategories();
        void PopulateSpecifications();
        void PopulateLegends();
        void Populate3DView();

        void UpdateAfterSave(std::shared_ptr<IProjectComponent> savedComponent);
        void UpdateVisableByIsCommand();
           
    private:
        QMargins m_border;
        QColor m_borderColor;
        int32_t m_widthOfParameterPanel;

    private:
        Gac3DViewer *m_pGac3DViewer = nullptr;
        QLineEdit *m_pLeComponentName = nullptr;
        QComboBox *m_pCbbSpec = nullptr;
        QPushButton *m_pBtnCreateNewSpec = nullptr;
        QPushButton *m_pBtnRenameSpec = nullptr;
        QPushButton *m_pBtnDeleteSpec = nullptr;
        LegendEditingCard *pPlanLegendCard = nullptr;
        LegendEditingCard *pElevationLegendCard = nullptr;
        LegendEditingCard *pProfileLegendCard = nullptr;
        LegendEditingCard *pElectricLegendCard = nullptr;
        EdittingParametersTableView *m_pGeoParametersTable = nullptr;
        ParametersEditingCard *pStandardParametersCard = nullptr;
        ParametersEditingCard *pCustomParametersCard = nullptr;
        QPushButton *pBtnCancel = nullptr;
        QPushButton *pBtnUpdatePreview = nullptr;
        QPushButton *pBtnSave = nullptr;
        CustomScroller *pProdParametersScroller = nullptr;
        CustomScroller *pGeoParametersScroller = nullptr;
        QLabel *m_pLblCategoryName = nullptr;

    private:
        std::shared_ptr<EditingProjectComponentContext> m_context;
        bool m_bSwitchCurrentSpecificationTo=true;
        bool m_isByCommandShow = false;
        bool m_isPrivateEnv = false;
    };
}